//
//  MusicViewController.m
//  音悦咖
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MusicViewController.h"
#import "C_MusicCircelLayout.h"
#import "C_MusicCircelCollectionViewCell.h"
#import "C_RecentTempCollectionViewCell.h"
#import "C_RecentLabelView.h"
#import "D_DataBase.h"


@interface MusicViewController ()<UIAlertViewDelegate>
@property (nonatomic, retain) NSMutableArray *CircelImageArr;
@property (nonatomic, retain) NSMutableArray *CircelTitleArr;
@property (nonatomic, retain) NSMutableArray *recentArr;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation MusicViewController

//static int Index = 1;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.Index = 6;
    
}

- (instancetype)initMusicView
{
    self = [super init];
    if (self) {
        [self createMainCollection];
        
    }
    return self;
}



- (void)createMainCollection
{

    [self getdata];
    [self getrecentArrData];
#pragma mark CircleCollection
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];

    self.mainCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 2, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];

#pragma mark 注册头部重用池
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 7);
    self.mainCollection.tag = 2001;
    self.mainCollection.delegate = self;
    self.mainCollection.dataSource = self;
    self.mainCollection.backgroundColor = [UIColor clearColor];
    [self.mainCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headre"];
#pragma mark TempCell重用池
    [self.mainCollection registerClass:[C_RecentTempCollectionViewCell class] forCellWithReuseIdentifier:@"C_RecentTempCollectionViewCell"];
#pragma mark RecentList 
    [self.mainCollection registerClass:[C_RecentListCollectionViewCell class] forCellWithReuseIdentifier:@"C_RecentListCollectionViewCell"];
    [self.view addSubview:self.mainCollection];
#pragma mark CircleCollection
    C_MusicCircelLayout *MuLayout = [[[C_MusicCircelLayout alloc] init] autorelease];
    self.CircleCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) collectionViewLayout:MuLayout];
    self.CircleCollection.backgroundColor = [UIColor clearColor];
    self.CircleCollection.delegate = self;
    self.CircleCollection.dataSource = self;
    self.CircleCollection.tag = 2000;
    
    [self.CircleCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"11"];
    [self.mainCollection addSubview:self.CircleCollection];
    [_CircleCollection release];
    
#pragma mark CircelCell重用池
    [self.CircleCollection registerClass:[C_MusicCircelCollectionViewCell class] forCellWithReuseIdentifier:@"C_MusicCircelCollectionViewCell"];
    [_mainCollection release];
    
#pragma mark  创建RecentLabel
    C_RecentLabelView *recent = [[C_RecentLabelView alloc] initWithFrame:CGRectMake(0, self.CircleCollection.frame.size.height, SCREEN_WIDTH, 40)];
    [recent.cleanButton addTarget:self action:@selector(cleanButton) forControlEvents:(UIControlEventTouchUpInside)];
    [self.mainCollection addSubview:recent];
    
}




#pragma mark 获得数据
- (void)getdata
{
    NSArray *arr = @[@{@"ImageName":@"NewSong", @"title":@"", @"url":@"http://online.dongting.com/recomm/new_songs_more?page=1&size=30&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music"},
                      @{@"ImageName":@"paihang", @"title":@"", @"url":@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=281&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music"},
                      @{@"ImageName":@"songer", @"title":@"", @"url":@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music"},
                      @{@"ImageName":@"Mv", @"title":@"", @"url":@"http://api.dongting.com/channel/channel/mvs?page=1&size=5&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=6860ec297111489677ad135e00a32421ada20951&idfa=D62E9175-00B7-4B64-AED1-5CFDD387D5D3&utdid=VZ8WeDZl8bYDAD0mj0MQyIEF&alf=201200&bundle_id=com.ttpod.music"},
                      @{@"ImageName":@"diantai", @"title":@"", @"url":@"http://fm.api.ttpod.com/radiolist?image_type=240_200&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music"},
                      @{@"ImageName":@"fenlei", @"title":@"", @"url":@"http://fm.api.ttpod.com/channellist?image_type=240_200&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music"}];
    
    self.dataArr = [MusicModel arrayWithModelByArray:arr];
    
    
 
}

#pragma mark GetrecentArr
- (void)getrecentArrData
{
    D_DataBase *recentData = [D_DataBase shareInstance];
    
    [recentData openDB];
    
    self.recentArr = [recentData selectInforRecent];
}

#pragma mark CleanButton方法
-(void)cleanButton
{

    if (self.recentArr.count) {
        [[[[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要删除数据嘛!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] autorelease] show];
    } else {
         [[[[UIAlertView alloc] initWithTitle:@"提示" message:@"亲你还没有浏览记录哦!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease] show];
    }
   

    NSLog(@"清除");
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 1: {
            
            D_DataBase *data = [D_DataBase shareInstance];
            [data openDB];
            [data deleteRecentTable];
            [data createTable];
            [self getrecentArrData];
            [self.mainCollection reloadData];
            break;
        }
        default:
            break;
    }
}

#pragma mark Collection Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (collectionView.tag)
            {
        case 2000:
            return self.dataArr.count;
            break;
        case 2001:
                    if (self.recentArr.count) {
                        return self.recentArr.count;
                    } else {
                        return 0;
                    }
            break;
        default:
            break;
    }
    
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2000: {
            C_MusicCircelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_MusicCircelCollectionViewCell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor greenColor];
            cell.model = self.dataArr[indexPath.item];
            return cell;
            break;
        }
        case 2001: {
            if (self.recentArr.count) {
                C_RecentListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_RecentListCollectionViewCell" forIndexPath:indexPath];
                cell.model = self.recentArr[indexPath.item];
                cell.backgroundColor = RGBA(110, 110, 110, 0.6);
                return cell;

            } else {
                C_RecentTempCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_RecentTempCollectionViewCell" forIndexPath:indexPath];

                
                cell.backgroundColor = [UIColor grayColor];
                return cell;
            }
            
            break;
        }
        case 2002:{
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"test" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor clearColor];
            return cell;
            break;
        }
        default:
            break;
    }
    
    return nil;
}
#pragma mark CollectonDelegateFlowLayout
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader && collectionView.tag == 2001) {
        UICollectionReusableView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headre" forIndexPath:indexPath];
     

        return reusable;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
   return CGSizeMake(SCREEN_WIDTH, 240);
   
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    //无限循环....
 
    if (_scrollView.tag == 2000) {
    
        _scrollView.pagingEnabled = YES;
    float targetX = _scrollView.contentOffset.x;
    NSInteger numCount = [self.CircleCollection numberOfItemsInSection:0];
    float ITEM_WIDTH = _scrollView.frame.size.width;
    
    if (numCount>=3)
    {
        if (targetX < ITEM_WIDTH/2) {
            [_scrollView setContentOffset:CGPointMake(targetX+ITEM_WIDTH *numCount, 0)];
        }
        else if (targetX >ITEM_WIDTH/2+ITEM_WIDTH *numCount)
        {
            [_scrollView setContentOffset:CGPointMake(targetX-ITEM_WIDTH *numCount, 0)];
        }
    }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == 2000) {
        
       self.Index = (int)(scrollView.contentOffset.x / SCREEN_WIDTH);
        // self.Index = &(Index);0
        
        NSLog(@"%ld",(long)self.Index);
        NSLog(@"%f", scrollView.contentOffset.x);
    }
    
    
    
}
//
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 2000: {

            [self.delegate MusicViewControllerPushViewWithCollectionView:collectionView ItemAtIndexPath:self.Index NewSongURL:[[self.dataArr[self.Index - 1] url] description]];
            break;
        }
        case 2001: {
            [self.delegate RecentSkimListPlayerWithModel:self.recentArr[indexPath.item]];
            
            break;
        }
        default:
            break;
    }
    
    
    
    
}

#pragma mark 自动轮播
-(void)circleChange
{
    static int num = 0;
    self.CircleCollection.contentOffset = CGPointMake(SCREEN_WIDTH * num, 0);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
